<template>
  <van-button
    v-if="!isFollowed"
    class="follow-btn"
    type="info"
    color="#3296fa"
    round
    size="small"
    icon="plus"
    @click="followedFn"
    :loading="isFollowLoading"
  >关注</van-button>
  <van-button
    v-else
    class="follow-btn"
    round
    size="small"
    @click="followedFn"
    :loading="isFollowLoading"
  >已关注</van-button>
</template>

<script>
import { addFollow, deleteFollow } from '@/api/user'
export default {
  name: 'FollowedIndex',
  props: {
    isFollowed: {
      type: Boolean,
      required: true
    },
    autId: {
      type: [Object, String, Number],
      required: true
    }
  },
  data () {
    return {
      isFollowLoading: false
    }
  },
  methods: {
    async followedFn () {
      this.isFollowLoading = true
      const autId = this.autId
      try {
        if (this.isFollowed) {
          await deleteFollow(autId)
          // console.log(res)
        } else {
          // 否则添加关注
          await addFollow(autId)
          // console.log(res)
        }
        //  else {
        //   const res = await addFollow(this.article.aut_id)
        //   console.log(res)
        // }
        // this.isFollowed = !this.isFollowed
        this.$emit('update:isFollowed', !this.isFollowed)
      } catch (err) {
        console.log(err)
        this.$toast('操作失败，请重试')
      }
      this.isFollowLoading = false
    }
  }
}
</script>

<style>
</style>
